<!DOCTYPE html><!--suppress ALL-->
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="imgtmp" th:inline="none">
            <img src="{{d.src}}"/>
        </script>

        <script type="text/html" id="isshow" th:inline="none">
            <input type="checkbox" title="{{d.title}}" lay-skin="switch" {{d.isshow?'checked':''}} value="{{d.id}}" lay-text="显|隐" lay-filter="status">
        </script>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="axios/axios.js"></script>
<script th:inline="none">
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        form.on('switch(status)', function (data) {
            let v = this.checked ? 1 : 0
            let id = data.value
            let t = this.title
            axios({
                url:'/updateisshow',
                method:'put',
                data:{v,id,t}
            }).then(res=>{
                layer.alert(res.data.msg)
            })

        })


        table.render({
            elem: '#currentTableId',
            url: '/api/carouseldb',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 20, title: 'ID', sort: true},
                {field: 'title', width: 100, title: '描述'},
                {field: 'src', width: 280, title: '图片', align: "center", templet: "#imgtmp"},
                {field: 'link', width: 30, title: '链接'},
                {field: 'create_time', width: 200, title: '添加时间'},
                {field: 'update_time', width: 200, title: '修改时间', sort: true},
                {field: 'isshow', width: 100, title: '显示', templet: "#isshow"},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加新轮播广告',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/cal/add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作,批量删除
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;

                //layer.alert(JSON.stringify(data));

                axios({
                    url:'/cal/delall',
                    method:'delete',
                    data:data
                }).then(res=>{

                }).catch(err=>console.log(err))
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //编辑事件处理
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/cal/edit',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                //删除事件处理
                layer.confirm(`真的删除 <strong>${obj.data.title}</strong> 吗？`, function (index) {
                    console.log(obj)
                    console.log(obj.data)
                    let id = obj.data.id
                    let src = 'static/'+obj.data.src
                    let t = obj.data.title
                    obj.del();
                    axios({
                        url:'/delete',
                        method:'delete',
                        data:{id,src,t}
                    }).then(res=>{
                        layer.msg(res.data.msg)
                    })
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>
